package com.xln.jiaju.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xln.jiaju.bean.Furn;
import com.xln.jiaju.bean.Msg;
import com.xln.jiaju.service.FurnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FurnController {

    @Autowired
    private FurnService furnService;

    @RequestMapping("/save")
    @ResponseBody
    public Msg furnSave(@Validated @RequestBody Furn furn, Errors errors){

        Map map = new HashMap();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(),fieldError.getDefaultMessage());
        }

        if (map.isEmpty()) {

            furnService.furnSave(furn);
            Msg success = Msg.success();
            return success;
        }else {
            return Msg.fail().add("errors",map);
        }
    }

    @RequestMapping("/furns")
    @ResponseBody
    public Msg queryAllFurn(){

        List<Furn> furnList = furnService.queryAll();
        // Msg msg = Msg.success();
        // Map<String, Object> furnMap = msg.getExtend();
        // furnMap.put("furnList",furnList);
        return Msg.success().add("furnList",furnList);

    }

    @RequestMapping("/updataFurn")
    @ResponseBody
    public Msg updateFurn(@RequestBody Furn furn){

        furnService.updateFurn(furn);
        Msg success = Msg.success();
        return success;

    }
    @RequestMapping("/deleteFurn/{id1}")
    @ResponseBody
    public Msg deleteFurn(@PathVariable("id1") int id){

        furnService.deleteFurn(id);
        Msg success = Msg.success();
        return success;

    }
    @RequestMapping("/pageFurnList")
    @ResponseBody
    public Msg pageFurnList(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        //1.调用findAll是完成查询，底层会进行物理分页，而不是逻辑分页
        // 2.会根据分页参数来计算limit?，?，在发出SQL语句时，会带limit
        // 13.我们后面会给大家抓取SQL
        List<Furn> furns = furnService.queryAll();
        PageInfo pageInfo = new PageInfo(furns,pageSize);
        Msg msg = Msg.success();
        msg.add("pageInfo",pageInfo);
        return msg;

    }

    @RequestMapping("/pageFurnListByName")
    @ResponseBody
    public Msg pageFurnListByName(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  @RequestParam(defaultValue = "") String name) {
        PageHelper.startPage(pageNum, pageSize);

        // 1.调用findAll是完成查询，底层会进行物理分页，而不是逻辑分页
        // 2.会根据分页参数来计算limit?，?，在发出SQL语句时，会带limit
        // 13.我们后面会给大家抓取SQL
        List<Furn> furns = furnService.queryFurnByName(name);
        PageInfo pageInfo = new PageInfo(furns, pageSize);
        Msg msg = Msg.success();
        msg.add("pageInfo", pageInfo);
        return msg;

    }
}
